package workflow

import (
	"fmt"
	hworkflow "zstackgo/pkg/header/core/workflow"
)

type ShareFlowChain struct {
	*SimpleFlowChain
	ShareFlows []ShareFlow
}

func NewShareFlowChain() *ShareFlowChain {
	return &ShareFlowChain{
		SimpleFlowChain: NewSimpleFlowChain(),
	}
}

func (c *ShareFlowChain) Then(flow hworkflow.Flow) *ShareFlowChain {
	if _, ok := flow.(ShareFlow); !ok {
		panic(fmt.Sprintf("ShareFlowChain only receives ShareFlow in then(), but %s got", flow.GetName()))
	}

	c.ShareFlows = append(c.ShareFlows, flow.(ShareFlow))

	return c
}

func (c *ShareFlowChain) Install(flow hworkflow.Flow) {
	c.SimpleFlowChain.Then(flow)
}

func (c *ShareFlowChain) Start() {
	if len(c.ShareFlows) == 0 {
		panic("you must call Then() to install ShareFlow before Start()")
	}

	for _, shareFlow := range c.ShareFlows {
		shareFlow.SetChain(c)
		shareFlow.Setup()
	}

	c.SimpleFlowChain.Start()
}
